package com.stars.controller.system;
import com.stars.common.annotation.UserMenuUpdateRedis;
import com.stars.common.easypoi.ExcelUtils;
import com.stars.entity.system.SysRoleEntity;
import com.stars.services.system.SysRoleAccessServices;
import com.stars.services.system.SysRoleConfigServices;
import com.stars.entity.dto.system.SysRoleDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import com.stars.common.annotation.Log;
import com.stars.common.base.BaseMsg;
import com.stars.common.ObjectCopyUtils;
import com.stars.common.base.BaseConstant;
import com.stars.common.base.ResultMsg;
import com.stars.controller.BaseController;
import com.stars.common.enums.OperationType;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.stars.common.valid.AddView;
import com.stars.common.valid.UpdateView;
import com.github.pagehelper.PageInfo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Title: 角色表、角色管理表 mybatis dao
 * @author dj
 * @date 2019-10-14 19:52:12
 * @version V1.0
 *
 */
@Slf4j
@RestController
@RequestMapping("/sysRoleCtl")
@Api(tags ="角色表、角色管理表")
public class SysRoleCtl extends BaseController{
   @Autowired
   private SysRoleAccessServices  sysRoleAccessServicesImpl;
   @Autowired
   private SysRoleConfigServices  sysRoleConfigServicesImpl;

   /**
   *  分页查询
   * @param pageNum 起始页
   * @param pageSize 大小
   * @return 分页数据
   * @throws Exception
   */
   @GetMapping("/pageList")
   @ApiOperation(value = "角色表、角色管理表"+BaseMsg.MODULAR_PAGE,notes ="角色表、角色管理表"+ BaseMsg.MODULAR_PAGE)
   @Log(detail = "角色表、角色管理表"+ BaseMsg.MODULAR_PAGE,operationType = OperationType.SELECT)
   public PageInfo<SysRoleDTO> pageSysRoleList(Integer pageNum, Integer pageSize, SysRoleDTO dto,HttpServletRequest request) throws Exception{
        setSystemApp(dto,request);
        return sysRoleAccessServicesImpl.pageList(pageNum,pageSize,dto);

   }

    /**
    * 保存
    * @param entityName
    * @return 成功失败
    * @throws Exception
    */
    @PostMapping(value = "/add")
    @ApiOperation(value = "角色表、角色管理表"+ BaseMsg.MODULAR_ADD,notes = "角色表、角色管理表"+BaseMsg.MODULAR_ADD)
    @Log(detail = "角色表、角色管理表"+BaseMsg.MODULAR_ADD,operationType = OperationType.INSERT)
    @UserMenuUpdateRedis(permission = true)
    public ResultMsg addSysRole(@Validated({AddView.class}) SysRoleEntity entityName,String [] addJson,HttpServletRequest request) throws Exception{
        setAddBaseApp(entityName,request);
        return sysRoleConfigServicesImpl.add(entityName,addJson);
    }
    /**
    * 更新
    * @param updateEntityDto
    * @return 成功失败
    * @throws Exception
    */
    @PostMapping(value = "/update")
    @ApiOperation(value = "角色表、角色管理表"+ BaseMsg.MODULAR_UPDATE,notes = "角色表、角色管理表"+ BaseMsg.MODULAR_ADD)
    @Log(detail = "角色表、角色管理表"+ BaseMsg.MODULAR_UPDATE,operationType = OperationType.UPDATE)
    @UserMenuUpdateRedis(permission = true)
    public ResultMsg updateSysRole(@Validated({UpdateView.class}) SysRoleEntity updateEntityDto,String [] addJson,String [] delJson,HttpServletRequest request) throws Exception{
        SysRoleEntity entity = sysRoleAccessServicesImpl.selectById(updateEntityDto.getId());
        ObjectCopyUtils.copyPropertiesIf(updateEntityDto,entity, BaseConstant.UPDATE);
        setUpdateBase(entity,request);
        return sysRoleConfigServicesImpl.update(entity,addJson,delJson);
    }

    /**
    * 根据id查询
    * @param id
    * @return 查询对象
    * @throws Exception
    */
    @GetMapping(value = "/findById")
    @ApiOperation(value = "角色表、角色管理表"+ BaseMsg.MODULAR_FIND_ID,notes = "角色表、角色管理表"+BaseMsg.MODULAR_FIND_ID)
    @Log(detail = "角色表、角色管理表"+ BaseMsg.MODULAR_FIND_ID,operationType = OperationType.SELECT)
    public ResultMsg findSysRoleById(String id) throws Exception{
        SysRoleEntity entityName = sysRoleAccessServicesImpl.selectById(id);
         return ResultMsg.success(entityName);
    }

     /**
     * 删除
     * @param ids
     * @return 成功失败
     * @throws Exception
     */
     @PostMapping(value = "/delById")
     @ApiOperation(value = "角色表、角色管理表"+BaseMsg.MODULAR_DEL,notes ="角色表、角色管理表"+BaseMsg.MODULAR_DEL)
     @Log(detail ="角色表、角色管理表"+BaseMsg.MODULAR_DEL,operationType = OperationType.DELETE)
     @UserMenuUpdateRedis(permission = true)
     public ResultMsg delSysRoleById(String ids) throws Exception{
         String [] idArray = ids.split(BaseConstant.SPOT);
         return sysRoleConfigServicesImpl.delByRoleIds(idArray);
     }

    /**
     * 导出
     * @param dto 查询条件
     * @param response
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/exportList")
    @ApiOperation(value = "角色表、角色管理表",notes ="角色表、角色管理表"+BaseMsg.MODULAR_SEARCH)
    @Log(detail ="角色表、角色管理表",operationType = OperationType.SELECT)
     public ResultMsg exportList(SysRoleDTO dto,HttpServletResponse response)throws Exception{
         // 模拟从数据库获取需要导出的数据
         List<SysRoleDTO> personList = sysRoleAccessServicesImpl.roleList(1,2000,dto);
         // 导出操作
         ExcelUtils.exportExcel(personList, "角色导出", "角色", SysRoleDTO.class, "角色.xls", response);
        return ResultMsg.success();
     }
    /**
     * 导入
     * @param file 查询条件
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/importExcel")
    @ApiOperation(value = "角色表、角色管理表",notes ="角色表、角色管理表"+BaseMsg.MODULAR_SEARCH)
    @Log(detail ="角色表、角色管理表",operationType = OperationType.SELECT)
    public ResultMsg importExcel(@RequestParam("file") MultipartFile file)throws Exception{
        // 导入操作
        List<SysRoleDTO> sysRoleDTOS = ExcelUtils.importExcel(file, 1, 1, SysRoleDTO.class);
        if (null != sysRoleDTOS){
            sysRoleDTOS.forEach(r ->{
                System.out.println(r.getRoleName());
            });
        }
        return ResultMsg.success();
    }


}